package ee.az.test.lighting;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class IconMapSerializer {

	public static String serialize(IconMap<IconSpot> map) {
		StringBuilder sb = new StringBuilder((map.getHeight() + 1) * map.getWidth());

		for (int y = 0; y < map.getHeight(); y++) {
			for (int x = 0; x < map.getWidth(); x++) {
				sb.append(map.getValue(x, y).getIcon());
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
	public static IconMap<IconSpot> deserialize(String string) {
		
		String[] rows = string.split("\n");
		int height = rows.length;
		int width = rows[0].length();
		
		IconMap<IconSpot> result = new IconMap<IconSpot>(width, height);
		
		for (int y = 0; y < result.getHeight(); y++) {
			for (int x = 0; x < result.getWidth(); x++) {
				char icon = rows[y].charAt(x);
				IconSpot spot = new IconSpotImpl(icon);
				result.setValue(x, y, spot);
			}
		}
		return result;
	}

}
